package eu.irreality.dai.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;

/*
 Created 27/08/2007 15:44:56
 */

/**
 * A list where objects are ordered by their priority value. Higher priorities
 * are better. <- o al rev�s?
 */
public class SortedList<EltType> implements Collection<EltType>
{

    private Comparator<? super EltType> cmp;
    private ArrayList<EltType> al;

    public Object clone()
    {
	SortedList<EltType> theClone = new SortedList<EltType>();
	theClone.cmp = cmp;
	theClone.al = (ArrayList<EltType>) al.clone();
	return theClone;
    }

    public SortedList()
    {
	this.cmp = null;
	this.al = new ArrayList<EltType>();
    }

    public SortedList(Comparator<? super EltType> cmp)
    {
	this.cmp = cmp;
	this.al = new ArrayList<EltType>();
    }

    private int compare(EltType obj1, EltType obj2) throws ClassCastException
    {
	if (cmp != null)
	    return cmp.compare(obj1, obj2);
	else
	    return ((Comparable<? super EltType>) obj1).compareTo(obj2);
    }

    public boolean add(EltType element)
    {
	int lim = al.size();
	int i = 0;
	while (i < lim && compare(al.get(i), element) < 0)
	    i++;
	al.add(i, element);
	return true;
    }

    public boolean addAll(Collection<? extends EltType> coll)
    {
	for (Iterator<? extends EltType> iter = coll.iterator(); iter.hasNext();)
	    add(iter.next());
	return true;
    }

    public void clear()
    {
	al.clear();
    }

    public boolean contains(Object element)
    {
	int lim = al.size();
	int i = 0;
	while (i < lim && !al.get(i).equals(element))
	    i++;
	if (i == lim)
	    return false; // reached end without finding eq element
	else
	    return true;
    }

    public boolean containsAll(Collection<?> coll)
    {
	for (Iterator<?> iter = coll.iterator(); iter.hasNext();)
	{
	    if (!contains(iter.next()))
		return false;
	}
	return true;
    }

    public boolean isEmpty()
    {
	return al.isEmpty();
    }

    public Iterator<EltType> iterator()
    {
	return al.iterator();
    }

    public EltType remove(int index)
    {
	return al.remove(index);
    }

    public boolean remove(Object obj)
    {
	return al.remove(obj);
    }

    public boolean removeAll(Collection<?> arg0)
    {
	return al.removeAll(arg0);
    }

    public boolean retainAll(Collection<?> arg0)
    {
	return al.retainAll(arg0);
    }

    public int size()
    {
	return al.size();
    }

    public Object[] toArray()
    {
	return al.toArray();
    }

    public <T> T[] toArray(T[] arg0)
    {
	return al.toArray(arg0);
    }

    public EltType get(int index)
    {
	return al.get(index);
    }

    public String toString()
    {
	StringBuffer sb = new StringBuffer("SortedList: ");
	for (Iterator<EltType> iter = this.iterator(); iter.hasNext();)
	{
	    EltType element = (EltType) iter.next();
	    sb.append(element.toString());
	    sb.append(" ");
	}
	return sb.toString();
    }

}
